package org.sja.log2traffic.dataStructures;

import java.util.HashMap;
import java.util.Map;



public class CounterForDateAndResponseTimeEntity {
	
	private String category;
	private Integer count;
	private Long spendTime;
	private DateAndResponseTimeEntity maxDateAndResponseTime;
	private Map<Integer,Integer> respTimeDistribution = new HashMap<Integer, Integer>(); 
	
	public CounterForDateAndResponseTimeEntity(String category){
		this.category = category;
		count = 0;
		spendTime = 0L;
		maxDateAndResponseTime = new DateAndResponseTimeEntity(0L, 0L);
	}

	public void newDateAndResponseTime(DateAndResponseTimeEntity dateAndResponseTime){
		if (maxDateAndResponseTime.getResponseTime() <= dateAndResponseTime.getResponseTime()){
			maxDateAndResponseTime = dateAndResponseTime;
		}
		modifyStatisticData((int)((dateAndResponseTime.getResponseTime()) / 100));
		++count;
		spendTime += dateAndResponseTime.getResponseTime();
	}

	private void modifyStatisticData(int shortTime) {
		
		Integer currentValue = respTimeDistribution.get(shortTime);
		if (currentValue == null){
			currentValue = 1;
		} else {
			++currentValue;
		}
		respTimeDistribution.put(shortTime, currentValue);
		
	}
	
	public Integer getCount() {
		return count;
	}
	
	public Long getSpendTime() {
		return spendTime;
	}

	public DateAndResponseTimeEntity getMaxDateAndTime() {
		return maxDateAndResponseTime;
	}

	public String getCategory() {
		return category;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((category == null) ? 0 : category.hashCode());
		result = prime * result + ((count == null) ? 0 : count.hashCode());
		result = prime * result + ((maxDateAndResponseTime == null) ? 0 : maxDateAndResponseTime.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CounterForDateAndResponseTimeEntity other = (CounterForDateAndResponseTimeEntity) obj;
		if (category == null) {
			if (other.category != null)
				return false;
		} else if (!category.equals(other.category))
			return false;
		if (count == null) {
			if (other.count != null)
				return false;
		} else if (!count.equals(other.count))
			return false;
		if (maxDateAndResponseTime == null) {
			if (other.maxDateAndResponseTime != null)
				return false;
		} else if (!maxDateAndResponseTime.equals(other.maxDateAndResponseTime))
			return false;
		return true;
	}

	public Map<Integer, Integer> getRespTimeDistribution() {
		return respTimeDistribution;
	}


}
